package stock

import (
	"gitee.com/lyuanbo/stock-plus/internal/svc"
	"gitee.com/lyuanbo/stock-plus/pkg/consts"
	storagememory "gitee.com/lyuanbo/stock-plus/pkg/stock/storage_memory"
	workermanager "gitee.com/lyuanbo/stock-plus/pkg/worker_manager"
	"github.com/nats-io/nats.go/jetstream"
)

var _ workermanager.Tasker = (*MAWorker)(nil)

func init() {
	// workermanager.Register(func(svcCtx *svc.ServiceContext, cache *storagememory.Cache) workermanager.Tasker {
	// 	return NewMAWorker(svcCtx, cache)
	// })
}

func NewMAWorker(svcCtx *svc.ServiceContext, cache *storagememory.Cache) *MAWorker {
	return &MAWorker{
		svcCtx:   svcCtx,
		customer: createCustomer(svcCtx.Ctx, "ma-worker", svcCtx.JetStream, "default", consts.TopicRealTime),
		cache:    cache,
	}
}

type MAWorker struct {
	svcCtx   *svc.ServiceContext
	customer jetstream.Consumer
	cache    *storagememory.Cache
}

func (m *MAWorker) Name() string {
	return "ma"
}

func (m *MAWorker) Init() {

}

func (m *MAWorker) Start() {
	go m.Run()
}

func (m *MAWorker) Run() {
	// _, _ = m.svcCtx.Nats.Subscribe(consts.TopicWorkerStart, func(msg *nats.Msg) {
	// 	var req WorkerStartReq
	// 	err := sonic.Unmarshal(msg.Data, &req)
	// 	if err != nil {
	// 		m.svcCtx.Logger.Error(m.svcCtx.Ctx, "unmarshal worker start req error", zap.Error(err))
	// 		return
	// 	}
	//
	// 	key := m.cache.GetKey(req.Symbol, req.Period)
	// 	// 获取平均线数据
	// 	rb := m.cache.GetRingBuffer(key)
	// 	rb.Mux.Lock()
	// 	defer rb.Mux.Unlock()
	//
	// 	sendM := make(map[string]struct{})
	// 	for _, ema := range m.svcCtx.Conf.Get().MA.Ema {
	// 		items := rb.GetLast(-ema)
	// 		if len(items) < ema {
	// 			continue
	// 		}
	//
	// 	}
	//
	// })
}
